package com.uchedao.vehicledetection.activity;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.uchedao.vehicledetection.R;
import com.uchedao.vehicledetection.adapter.CityListAdapter;
import com.uchedao.vehicledetection.model.Address;
import com.uchedao.vehicledetection.utils.city.CityUtil;
import com.uchedao.vehicledetection.view.baseinfo.BaseInfoCarInfoView;


public class ProvinceListActivity extends CJDBaseActivity {

    private static int REQ_PICK_CITY = 0x102;

    private ListView lvProvince;

    private CityUtil cityUtil;

    private Address address;
    @Override
    protected String getActivityTag() {
        return "ProvinceListActivity";
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_province_list;
    }

    @Override
    protected void initView() {
        lvProvince = (ListView) findViewById(R.id.lvProvince);
    }

    @Override
    protected void initData() {
        setTitle("城市选择");
        cityUtil = CityUtil.getInstance(this);
        lvProvince.setAdapter(new CityListAdapter(this, cityUtil.mProvinceDatas));
        address = new Address();
    }

    @Override
    protected void initListener() {
        findViewById(R.id.title_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        lvProvince.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                Intent it = new Intent(ProvinceListActivity.this, CityListActivity.class);
                address.setProvince(cityUtil.mProvinceDatas.get(i));
                // id?
                it.putExtra(BaseInfoCarInfoView.KEY_DATA, address);
                startActivityForResult(it, REQ_PICK_CITY);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==REQ_PICK_CITY && resultCode==RESULT_OK) {
            setResult(RESULT_OK, data);
            finish();
        }
    }
}
